#include "game.h"

int x;
int y;

void Menu()
{
  printf("############################################\n");
  printf("#####   1.Play       0.Exit   ##############\n");
  printf("############################################\n");
  printf("############################################\n");
  printf("Please Select");
}

int ChressCount(int board[][COL],int row,int col,enum Dir d)
{
  int _x = x-1;//from 1 start
  int _y = y-1;//from 1 start
  
  int count = 0;
  while(1)
  {
    switch(d)
    {
      case LEFT:
        _y--;
        break;
      case RIGHT:
        _y++;
        break;
      case UP:
        _x++;
        break;
      case DOWN:
        _x--;
        break;
      case LEFT_UP:
        _x++;
        _y--;
        break;
      case LEFT_DOWN:
        _x--;
        _y--;
        break;
      case RIGHT_UP:
        _x++;
        _y++;
        break;
      case RIGHT_DOWN:
        _x--;
        _y++;
        break;
      default:
        break;
    }
    if(_x<0||_x>row-1||_y<0||_y>col)
    {
      break;
    }
    if(board[x-1][y-1]==board[_x][_y])//_x and _y has changed
    {
      count++;
    }
    else{
      break;
    }
  }
  return count;
}

int IsOver(int board[][COL],int row,int col)
{
  int count1 = ChressCount(board,row,col,LEFT)+ChressCount(board,row,col,RIGHT)+1;
  int count2 = ChressCount(board,row,col,UP)+ChressCount(board,row,col,DOWN)+1;
  int count3 = ChressCount(board,row,col,LEFT_UP)+ChressCount(board,row,col,RIGHT_DOWN)+1;
  int count4 = ChressCount(board,row,col,LEFT_DOWN)+ChressCount(board,row,col,RIGHT_UP)+1;

  if(count1==5||count2==5||count3==5||count4==5)
  {
    if(board[x-1][y-1]==1)
    {
      return PLAY1_WIN;
    }
    return PLAY2_WIN;
  }
  int i = 0;
  int j = 0;
  for(;i<row;i++)
  {
    for(;j<col;j++)
    {
      if(board[i][j]==0)
      {
        return NEXT;
      }
    }
  }
  return DRAW;
}


void ShowBoard(int board[][COL],int row,int col)
{
  //keshihua
  printf("\e[1;1H\e[2J");//qingping
  int i = 1;
  printf("  ");
  for(;i<=col;i++)
  {
    printf("%3d",i);
  }
  printf("\n");

  for(i=0;i<row;i++)
  {
    int j = 0;
    printf("%2d ",i+1);//%2d+ kongge make it aline
    for(;j<col;j++)
    {
      if(board[i][j]==0)
      {
        printf(" . ");
      }
      else if(board[i][j]==PLAYER1)
      {
        printf(" ● ");
      }
      else{
        printf(" ○ ");
      }
    }
    printf("\n");
  }
}
void PlayMove(int board[][COL],int row,int col,int who)
{
  while(1)
  {
    printf("Player[%d]Please Enter Your Pos#",who);
    scanf("%d%d",&x,&y);
    if(x>0 && x<row && y>0 && y<col)
    {
      if(board[x-1][y-1]==0)
      {
        board[x-1][y-1]=who;
        break;
      }
      else{
        printf("Pos Is Occupied!\n");
        break;
      }
    }
    else{
      printf("Pos Is Not Right!\n");
      break;
    }
  }
}
void Game()
{
  int board[ROW][COL];
  memset(board,0,sizeof(board));
  int result = 0;
  do{
      ShowBoard(board,ROW,COL);
      PlayMove(board,ROW,COL,PLAYER1);
      result = IsOver(board,ROW,COL);
      if(NEXT!=result)
      {
        break;
      }

      ShowBoard(board,ROW,COL);
      PlayMove(board,ROW,COL,PLAYER2);
      result = IsOver(board,ROW,COL);
      if(NEXT!=result)
      {
        break;
      }

  }while(1);
  //p1 win  p2 win   draw
  switch(result)
  {
    case PLAY1_WIN:
      printf("Congratulate Play1 winning!\n");
      break;
    case PLAY2_WIN:
      printf("Congratulate Play2 winning!\n");
      break;
    case DRAW:
      printf("Drawing!\n");
      break;
    default:
      //do nothing
      break;
  }
}
